<?php

namespace App\Http\Controllers\outline;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\CourseValidateRequest;
use App\Http\Requests\postscheck;
use App\Http\Requests\userscheck;


class ViewTheArticle extends Controller
{
    //
    public function SpecialColumn()
    {

        $data=DB::table('tags')->select('tag_name')->get();
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
             return response()->fail(100,'失败');
        }


    }
    public function ConterntMessage()
    {
        $data=DB::table('users as U' )->join('posts as P','U.id', 'P.user_id')->select('body','title','like','view','p.created_at','user_name')->paginate(5);
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
            return response()->fail(100,'失败');
        }
    }
    public function ColumnMessage(userscheck $request)
    {
        $id=$request->id;
        $data=DB::table('users as U' )->join('posts as P','U.id', 'P.user_id')->where('p.id',$id)->select('user_name','body','title','p.created_at','column_id')->get();
        if($data!=null){
            return response()->success(200,"成功",$data);
        }
        else{
            return response()->fail(100,'失败');
        }
    }
    public function Thumbs(postscheck $request)
    {

        $pull_id=$request->pull_id;
        $push_id=$request->push_id;
        $post_id=$request->post_id;
        if($request->isMethod('post'))
        {
            $msg=DB::table('messages')->where('push_id',$push_id)->value('msg');
           $data= DB::table('posts as p')->join('post_tags as t','p.id','t.post_id')->where('p.id',$post_id)->increment('like');
           $db=DB::table('posts as p')->join('post_tags as t','p.id','t.post_id')->where('p.id',$post_id)->value('like');
            if($data!=null){
                if($db!=0 && $db%10==0)
                {

                    DB::table('users as u')->join('posts as p','u.id','p.user_id')->where('user_id',$pull_id)->increment('rating',2);
                    $r=DB::table('messages as m')->join('posts as p','m.id','p.user_id');
                    $result=$r->where('pull_id',$pull_id)->update(['msg'=>"您的点赞满了{$db}积分加2"]);

                }

                return response()->success(200,"成功",$msg);
            }
            else{
                return response()->fail(100,'失败');
            }
        }
    }
    public function CancelThumbs(postscheck $request)
    {
        $pull_id=$request->pull_id;
        $push_id=$request->push_id;
        $post_id=$request->post_id;
        if($request->isMethod('post'))
        {
            $msg=DB::table('messages')->where('push_id',$push_id)->delete();
            $data= DB::table('posts as p')->join('post_tags as t','p.id','t.post_id')->where('p.id',$post_id)->decrement('like');
            $db=DB::table('posts as p')->join('post_tags as t','p.id','t.post_id')->where('p.id',$post_id)->value('like');

            if($data!=null){
                if($db!=0 && ($db+1)%10==0)
                {
                    DB::table('users as u')->join('posts as p','u.id','p.user_id')->where('user_id',$pull_id)->decrement('rating',2);
                    $msg=DB::table('messages as m')->join('posts as p','m.id','p.user_id');
                    $result=$msg->where('pull_id',$pull_id)->delete();
                }
                $r="取消赞成功";

                return response()->success(200,"成功",$r);
            }
            else{
                return response()->fail(100,'失败');
            }
        }
    }
}
